<template>
    <el-dialog v-model="dialogFormVisible" :title="title" @close="closeDialog">
        <el-form :model="addApprovalDisciplineForm" ref="addApprovalDisciplineRef" :rules="addApprovalDisciplinerules">
                        <el-form-item label="行为名称" :label-width="formLabelWidth"
                                      prop="disciplineTypeName">
                            <el-input
                                    v-model="addApprovalDisciplineForm.disciplineTypeName"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写行为名称"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="行为分数" :label-width="formLabelWidth"
                                      prop="disciplineScore" v-if="addApprovalDisciplineForm.id">
                            <el-input
                                    v-model="addApprovalDisciplineForm.disciplineScore"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写行为分数"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="审批状态" :label-width="formLabelWidth"
                                      prop="status" v-if="addApprovalDisciplineForm.id">
                            <el-input
                                    v-model="addApprovalDisciplineForm.status"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写审批状态"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="情况说明" :label-width="formLabelWidth"
                                      prop="detail">
                            <el-input
                                    v-model="addApprovalDisciplineForm.detail"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写情况说明"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="图片" :label-width="formLabelWidth"
                                      prop="images">
                            <el-input
                                    v-model="addApprovalDisciplineForm.images"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写图片"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="班级" :label-width="formLabelWidth"
                                      prop="classId" v-if="addApprovalDisciplineForm.id">
                            <el-input
                                    v-model="addApprovalDisciplineForm.classId"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写班级"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="年级" :label-width="formLabelWidth"
                                      prop="gradeId" v-if="addApprovalDisciplineForm.id">
                            <el-input
                                    v-model="addApprovalDisciplineForm.gradeId"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写年级"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="学生学号" :label-width="formLabelWidth"
                                      prop="studentNumber">
                            <el-input
                                    v-model="addApprovalDisciplineForm.studentNumber"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写学生学号"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="学生姓名" :label-width="formLabelWidth"
                                      prop="studentName">
                            <el-input
                                    v-model="addApprovalDisciplineForm.studentName"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写学生姓名"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="提交人工号" :label-width="formLabelWidth"
                                      prop="submitTeacherNumber">
                            <el-input
                                    v-model="addApprovalDisciplineForm.submitTeacherNumber"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写提交人工号"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="校区" :label-width="formLabelWidth"
                                      prop="campus" v-if="addApprovalDisciplineForm.id">
                            <el-input
                                    v-model="addApprovalDisciplineForm.campus"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写校区"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="提交时间" :label-width="formLabelWidth"
                                      prop="submitDate" v-if="addApprovalDisciplineForm.id">
                            <el-input
                                    v-model="addApprovalDisciplineForm.submitDate"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写提交时间"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="违纪时间" :label-width="formLabelWidth"
                                      prop="date">
                            <el-input
                                    v-model="addApprovalDisciplineForm.date"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写违纪时间"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="同一次提交唯一标识" :label-width="formLabelWidth"
                                      prop="uuid">
                            <el-input
                                    v-model="addApprovalDisciplineForm.uuid"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写同一次提交唯一标识"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="级部名称" :label-width="formLabelWidth"
                                      prop="subordinateDepartmentName">
                            <el-input
                                    v-model="addApprovalDisciplineForm.subordinateDepartmentName"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写级部名称"
                            ></el-input>
                        </el-form-item>
        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel()">取消</el-button>
        <el-button type="primary" @click="save()">保存</el-button>
      </span>
        </template>
    </el-dialog>
</template>

<script>
    import {ref} from "vue";
    import {httpService} from "@/utils/httpService";
    import {ElMessage, ElForm} from "element-plus";
    import {vxRule} from "@/utils/validator";

    export default {
        name: "AddApprovalDiscipline",
        components: {ElForm, ElMessage},
        data() {
            return {
                dialogFormVisible: ref(false),
                formLabelWidth: "120px",
                title: "",
                personnelType: "",
                addApprovalDisciplineForm: {
                    disciplineTypeName: "",
                    disciplineScore: "",
                    status: "",
                    detail: "",
                    images: "",
                    classId: "",
                    gradeId: "",
                    studentNumber: "",
                    studentName: "",
                    submitTeacherNumber: "",
                    campus: "",
                    submitDate: "",
                    date: "",
                    uuid: "",
                    subordinateDepartmentName: "",
                },
                // 校验规则
                addApprovalDisciplinerules: {
                    disciplineTypeName: vxRule(true, null, "blur"),
                    detail: vxRule(true, null, "blur"),
                    images: vxRule(true, null, "blur"),
                    studentNumber: vxRule(true, null, "blur"),
                    studentName: vxRule(true, null, "blur"),
                    submitTeacherNumber: vxRule(true, null, "blur"),
                    date: vxRule(true, null, "blur"),
                    uuid: vxRule(true, null, "blur"),
                    subordinateDepartmentName: vxRule(true, null, "blur"),
                },
            };
        },
        methods: {
            closeDialog() {
                this.clear();
            },
            cancel() {
                this.dialogFormVisible = false;
                this.clear();
            },
            getApprovalDisciplineById(id) {
                httpService("/api/approvalDiscipline/queryById", {id: id}, "get").then(
                    (data) => {
                        this.addApprovalDisciplineForm = data;
                    }
                );
            },
            clear() {
                this.dialogFormVisible = false;
                //清空form
                    this.addApprovalDisciplineForm.id = "";
                    this.addApprovalDisciplineForm.disciplineTypeName = "";
                    this.addApprovalDisciplineForm.disciplineScore = "";
                    this.addApprovalDisciplineForm.status = "";
                    this.addApprovalDisciplineForm.detail = "";
                    this.addApprovalDisciplineForm.images = "";
                    this.addApprovalDisciplineForm.classId = "";
                    this.addApprovalDisciplineForm.gradeId = "";
                    this.addApprovalDisciplineForm.studentNumber = "";
                    this.addApprovalDisciplineForm.studentName = "";
                    this.addApprovalDisciplineForm.submitTeacherNumber = "";
                    this.addApprovalDisciplineForm.campus = "";
                    this.addApprovalDisciplineForm.submitDate = "";
                    this.addApprovalDisciplineForm.date = "";
                    this.addApprovalDisciplineForm.uuid = "";
                    this.addApprovalDisciplineForm.subordinateDepartmentName = "";
            },
            save() {
                this.$refs.addApprovalDisciplineRef.validate((valid) => {
                    if (valid) {
                        let params = new FormData(); //创建一个form对象,必须是form对象否则后端接受不到数据
                        params.append("disciplineTypeName", this.addApprovalDisciplineForm.disciplineTypeName);
                        params.append("disciplineScore", this.addApprovalDisciplineForm.disciplineScore);
                        params.append("status", this.addApprovalDisciplineForm.status);
                        params.append("detail", this.addApprovalDisciplineForm.detail);
                        params.append("images", this.addApprovalDisciplineForm.images);
                        params.append("classId", this.addApprovalDisciplineForm.classId);
                        params.append("gradeId", this.addApprovalDisciplineForm.gradeId);
                        params.append("studentNumber", this.addApprovalDisciplineForm.studentNumber);
                        params.append("studentName", this.addApprovalDisciplineForm.studentName);
                        params.append("submitTeacherNumber", this.addApprovalDisciplineForm.submitTeacherNumber);
                        params.append("campus", this.addApprovalDisciplineForm.campus);
                        params.append("submitDate", this.addApprovalDisciplineForm.submitDate);
                        params.append("date", this.addApprovalDisciplineForm.date);
                        params.append("uuid", this.addApprovalDisciplineForm.uuid);
                        params.append("subordinateDepartmentName", this.addApprovalDisciplineForm.subordinateDepartmentName);
                        if (this.addApprovalDisciplineForm.id) {
                            params.append("id", this.addApprovalDisciplineForm.id);
                            httpService("/api/approvalDiscipline/edit", params, "post").then(
                                (data) => {
                                    if (data.code == 200) {
                                        ElMessage.success("修改成功");
                                        this.clear();
                                        //刷新父组件的数据
                                        this.$parent.getApprovalDisciplineList();
                                    } else {
                                        ElMessage.error("修改失败");
                                    }
                                }
                            );
                        } else {
                            httpService("/api/approvalDiscipline/add", params, "post").then(
                                (data) => {
                                    if (data.code == 200) {
                                        ElMessage.success("添加成功");
                                        this.clear();
                                        //刷新父组件的数据
                                        this.$parent.getApprovalDisciplineList();
                                    } else {
                                        ElMessage.error("添加失败");
                                    }
                                }
                            );
                        }
                    } else {
                        return false;
                    }
                });
            },
        },
    };
</script>
<style>
</style>